package com.wfh.activity;

import com.wfh.R;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ScrollView;
import android.widget.TextView;

public class SettingActivity extends CommonActivity implements OnClickListener {

	private TextView tixing, jifen, banquan, qiye, lianxi, banben, jiancha;
	private Dialog alertDialog;
	private TextView content;
	private BroadcastReceiver datareceiver;
	private Dialog downDialog;
	private MyDialog dialog;
	private boolean mThread=true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_setting);
		tixing = (TextView) findViewById(R.id.tixing);
		jifen = (TextView) findViewById(R.id.jifen);
		banquan = (TextView) findViewById(R.id.banquan);
		qiye = (TextView) findViewById(R.id.qiye);
		lianxi = (TextView) findViewById(R.id.lianxi);
		banben = (TextView) findViewById(R.id.banben);
		jiancha = (TextView) findViewById(R.id.jiancha);
		
		Log.i("setting",this.toString());
		
		banben.setText("当前版本   "+Util.getVersionName(this));

		createDialog();// 创建dialog
		dialog=MyDialog.create(this);

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_SETTING);
		datareceiver = new DataReceiver();
		registerReceiver(datareceiver, filter);

		tixing.setOnClickListener(this);
		jifen.setOnClickListener(this);
		banquan.setOnClickListener(this);
		qiye.setOnClickListener(this);
		lianxi.setOnClickListener(this);
		banben.setOnClickListener(this);
		jiancha.setOnClickListener(this);

	}

	// 点击操作
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.banquan:
			if(!mThread) return;
			mThread=false;
			content.setText(Util.getProperty(SettingActivity.this, "banquan"));
			alertDialog.setTitle("版权说明");
			alertDialog.show();
			mThread=true;
			break;
		case R.id.qiye:
			if(!mThread) return;
			mThread=false;
			HomeActivity.service.getInfo(SettingActivity.this, null);
			break;
		case R.id.lianxi:
			if(!mThread) return;
			mThread=false;
			content.setText(Util.getProperty(SettingActivity.this, "contact"));
			alertDialog.setTitle("联系我们");
			alertDialog.show();
			mThread=true;
			break;
		case R.id.jiancha:
			if(!mThread) return;
			mThread=false;
			HomeActivity.service.getNewVersion(this, null);
			break;
		case R.id.tixing:
//			Util.startactivity(SettingActivity.this, LoginActivity.class);
//			Util.startactivity(SettingActivity.this, RegistActivity.class);
			break;
		case R.id.jifen:
			if(!mThread) return;
			mThread=false;
			HomeActivity.service.getScore(this, null);
			content.setText("正在载入……");
			alertDialog.setTitle("您的积分是");
			alertDialog.show();
			break;
		default:
			break;
		}

	}

	public void createDialog() {
		ScrollView scrollview = new ScrollView(this);
		scrollview.setBackgroundColor(Color.WHITE);
		content = new TextView(this);
		content.setTextColor(Color.BLACK);
		content.setPadding(10, 10, 10, 10);
		content.setWidth(400);
		scrollview.addView(content);
		alertDialog = Util.alertDialog(SettingActivity.this, scrollview, "", "");

	}

	public void downDialog(final String file_url,final String content){
		downDialog = new AlertDialog.Builder(
				SettingActivity.this)
				.setTitle("有新的版本")
				.setPositiveButton("立即更新",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(
									DialogInterface alertdialog,
									int which) {
								if(file_url==null) return;
								Bundle bd=new Bundle();
								bd.putString("file_url", file_url);
								HomeActivity.service.getApk(SettingActivity.this, new Intent().putExtras(bd));
								alertdialog.dismiss();
								dialog.show();
							}
						})
				.setNegativeButton("稍后再说",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(
									DialogInterface dialog,
									int which) {
								downDialog.dismiss();
								mThread=true;
							}
						})
				.setMessage(Util.replaceHtml(content))
				.create();
		downDialog.show();
	}

	// 接收器
	private class DataReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			if(bd==null){
				Util.showToast(SettingActivity.this, "请检查您的网络");
				mThread=true;
				return;
			}
			switch (bd.getInt("action")) {
				case 1:// 店铺介绍
					content.setText(bd.getString("info"));
					alertDialog.setTitle("店铺介绍");
					alertDialog.show();
					mThread=true;//释放thread
					break;
				case 2://检查更新
					String ret=bd.getString("ret");
					if(ret.equals(0)){
						Util.showToast(SettingActivity.this, "暂无新版本");
					}else{
						if(!Util.getVersionName(SettingActivity.this).equalsIgnoreCase(bd.getString("version_code"))){
							downDialog(bd.getString("file_url"),bd.getString("bak"));//开始下载
						}else{
							Util.showToast(SettingActivity.this, "暂无新版本");
						}
					}
					break;
				case 3://更新下载进度
					String percent=bd.getString("percent");
					Log.i("percent",percent);
					dialog.setText("已下载"+percent);
					if(percent.equals("100.0%")){
						dialog.hide();
						mThread=true;//下载完后才释放
					}
					break;
				case 4://获取积分
					if(bd.getString("ret").equals("100")){
						content.setText("此用户不存在");
					}else if(bd.getString("ret").equals("1")){
						content.setText(bd.getString("score"));
					}else{
						Util.showToast(SettingActivity.this, "积分获取错误");
					}
					mThread=true;//释放thread
					break;
				default:
					break;
			}
		}
	}

	// 监听返回键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "退出");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			android.os.Process.killProcess(android.os.Process.myPid());
			System.gc();
			System.exit(0);
			notify();
			break;
		default:
			break;
		}
		return true;
	}
	
	@Override
	protected void onDestroy() {
		if(downDialog!=null) downDialog.cancel();
		if(alertDialog!=null) alertDialog.cancel();
		if(dialog!=null) dialog.cancel();
		if(datareceiver!=null) unregisterReceiver(datareceiver);
		super.onDestroy();
	}

}
